package com.photoeditor.demo.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.base.log.DLog;
import com.common.base.utils.thread.ThreadPool;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.photoeditor.R;
import com.photoeditor.demo.app.XActivity;
import com.photoeditor.demo.imageloader.ImageLoader;
import com.photoeditor.demo.model.bean.BabyInputBean;
import com.photoeditor.demo.model.bean.face.BabyPredicResultBean;
import com.photoeditor.demo.model.bean.face.ScanInfo;
import com.photoeditor.demo.model.face.BabyInfoInputDialog;
import com.photoeditor.demo.model.face.EthnicitySelectDialog;
import com.photoeditor.demo.ui.activity.face.FaceLogicActivity;
import com.photoeditor.demo.ui.fragement.HomeFragment;
import com.photoeditor.demo.util.FontUtil;
import com.photoeditor.demo.util.JsonCallback;
import com.photoeditor.demo.util.SignUtils;
import com.photoeditor.demo.util.ToastUtil;
import com.photoeditor.demo.util.face.FaceUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.photoeditor.demo.model.face.FaceConstant.ACTION_FUTURE_BABY;
import static com.photoeditor.demo.ui.activity.FaceAdjustActivity.EXTRA_IMG_PATH;
import static com.photoeditor.demo.ui.activity.face.FaceLogicActivity.REQUEST_CODE_CHOOSE_PHOTO_YOU;

public class BabyMainActivity extends XActivity {
    @Bind(R.id.iv_father)
    ImageView mIvFather;
    @Bind(R.id.iv_mother)
    ImageView mIvMother;
    @Bind(R.id.iv_father_2)
    ImageView mIvFather2;
    @Bind(R.id.iv_mother_2)
    ImageView mIvMother2;
    @Bind(R.id.tv_father)
    TextView mTvFather;
    @Bind(R.id.tv_mother)
    TextView mTvMother;
    @Bind(R.id.btn_get_my_result)
    Button mBtnNext;

    public static final int REQUEST_CODE_CHOOSE_PHOTO_FATHER = 100;
    public static final int REQUEST_CODE_CHOOSE_PHOTO_MOTHER = 101;
    public static final int REQUEST_CODE_ADJUST_PHOTO_FATHER = 102;
    public static final int REQUEST_CODE_ADJUST_PHOTO_MOTHER = 103;
    public static final int RESULT_CODE_ADJUST_PHOTO_FATHER = 202;
    public static final int RESULT_CODE_ADJUST_PHOTO_MOTHER = 203;
    public static final String ACTION_PREDICT_BABY_FATHER = "ACTION_PREDICT_BABY_FATHER";
    public static final String ACTION_PREDICT_BABY_MOTHER = "ACTION_PREDICT_BABY_MOTHER";

    private final String TAG = BabyMainActivity.class.getSimpleName();

    private String mFatherPicPath;
    private String mMotherPicPath;
    private BabyInputBean mBabyInputBean;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, BabyMainActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baby_main);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        FontUtil.setCustomFontMedium(mTvFather, mTvMother);
        setBtnState();
    }

    @OnClick({R.id.iv_father_2, R.id.iv_mother_2})
    public void onIconClicked(View view) {
        Intent intent = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, this, GalleryActivity.class);
        intent.setType("image/*");
        intent.putExtra(GalleryActivity.EXTRA_ENTRANCE, "baby_main");
        intent.putExtra(HomeFragment.EXTRA_MAIN_BTN_ENTRANCE, "face");
        switch (view.getId()) {
            case R.id.iv_father_2:
                startActivityForResult(intent, REQUEST_CODE_CHOOSE_PHOTO_FATHER);
                break;
            case R.id.iv_mother_2:
                startActivityForResult(intent, REQUEST_CODE_CHOOSE_PHOTO_MOTHER);
                break;
            default:
                break;
        }
    }

    @OnClick(R.id.back)
    public void onBackClicked(View view) {
        onBackPressed();
    }

    @OnClick(R.id.btn_get_my_result)
    public void onGetMyResultClicked(View view) {
        showInputDialog();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void showInputDialog() {
        BabyInfoInputDialog dialog = new BabyInfoInputDialog();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        dialog.show(getSupportFragmentManager(), "dl");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void requestBabyReportData(BabyInputBean inputBean) {
        if (inputBean != null) {
            mBabyInputBean = inputBean;
            FaceLogicActivity.showReportActivity(
                    this,
                    ACTION_FUTURE_BABY,
                    1,//从相册中选择
                    inputBean,
                    mFatherPicPath,
                    mMotherPicPath);
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CODE_CHOOSE_PHOTO_FATHER:
                if (data != null && data.getData() != null) {
                    FaceAdjustActivity.startFaceAdjustActivity(
                            this,
                            data.getData(),
                            ACTION_PREDICT_BABY_FATHER,
                            1,
                            false,
                            true,
                            REQUEST_CODE_ADJUST_PHOTO_FATHER);
                }
                break;
            case REQUEST_CODE_CHOOSE_PHOTO_MOTHER:
                if (data != null && data.getData() != null) {
                    FaceAdjustActivity.startFaceAdjustActivity(
                            this,
                            data.getData(),
                            ACTION_PREDICT_BABY_MOTHER,
                            1,
                            false,
                            true,
                            REQUEST_CODE_ADJUST_PHOTO_MOTHER);
                }
                break;
            case REQUEST_CODE_ADJUST_PHOTO_FATHER:
                if (data != null && !TextUtils.isEmpty(data.getStringExtra(EXTRA_IMG_PATH))) {
                    mFatherPicPath = data.getStringExtra(EXTRA_IMG_PATH);
                    mIvFather2.setVisibility(View.GONE);
                    ImageLoader.loadImage(mFatherPicPath, mIvFather);
                }
                setBtnState();
                break;
            case REQUEST_CODE_ADJUST_PHOTO_MOTHER:
                if (data != null && !TextUtils.isEmpty(data.getStringExtra(EXTRA_IMG_PATH))) {
                    mMotherPicPath = data.getStringExtra(EXTRA_IMG_PATH);
                    mIvMother2.setVisibility(View.GONE);
                    ImageLoader.loadImage(mMotherPicPath, mIvMother);
                }
                setBtnState();
                break;
            default:
                break;
        }
    }

    private void setBtnState() {
        if (!TextUtils.isEmpty(mFatherPicPath) && !TextUtils.isEmpty(mMotherPicPath)) {
            mBtnNext.setBackgroundResource(R.drawable.bg_shape_round_conner_blue_gradient);
            mBtnNext.setClickable(true);
        } else {
            mBtnNext.setBackgroundResource(R.drawable.bg_shape_round_conner_gray);
            mBtnNext.setClickable(false);
        }
    }
}
